/**
 * Created by sicongdu on 17-12-19.
 */

var signup_validate = () => {
    var name = document.getElementById("name").value
    var stNumber = document.getElementById("stNumber").value
    var classNo = document.getElementById("classNo").value
    var password = document.getElementById("Possword").value
    var password2 = document.getElementById("password2").value

    if(stNumber.length != 10){
        alert("学号格式错误!")
    }else if(password == ""){
        alert("密码不能为空!")
    }else if(name == ""){
        alert("姓名不能为空!")
    }else if(classNo.length != 10){
        alert("班级号格式错误!")
    }else if(password != password2) {
        alert("两次密码输入不一致!")
    }else {
        var http_request = new XMLHttpRequest();
        try{
            // Opera 8.0+, Firefox, Chrome, Safari
            http_request = new XMLHttpRequest();
        }catch (e){
            // Internet Explorer Browsers
            try{
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            }catch (e) {
                try{
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                }catch (e){
                    // Something went wrong
                    alert("Your browser broke!");
                    return false;
                }
            }
        }

        http_request.onreadystatechange  = () => {
            if (http_request.readyState == 4  )
            {
                // Javascript function JSON.parse to parse JSON data
                var jsonObj = JSON.parse(http_request.responseText);

                // jsonObj variable now contains the data structure and can
                // be accessed as jsonObj.name and jsonObj.country.
                var flag = jsonObj.pass
                if(flag == 0){
                    alert("注册成功！")
                    window.location.href = 'http://www.ourspark.org/index';
                }else if(flag == 1){
                    alert("注册失败！请重新注册！")
                }
            }
        }

        // 如果用的是post方式请求，要在send之前设置HTTP头
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        http_request.open("POST", "http://www.ourspark.org/sign_up_save", true);

        var data_json = {
            name: name,
            stNumber: stNumber,
            classNo: classNo,
            password: password
        }
        http_request.send(JSON.stringify(data_json));
    }
}
